$(function() {
    getCartList();
    getAddressList();

//通过点击事件获得地址信息
    $(document).on('click', '#addrId_674944241' , function () {
        let value =$(this).attr("data-address-id");
        let name =$(this).attr("data-address-name");
        let detail =$(this).attr("data-address-detail");

        $("#page").attr("data-current_address_id",value)
            .attr("data-current_address_name",name)
            .attr("data-current_address_detail",detail);

        $("#J_AddrConfirm").html(detail);
        $("#J_AddrNameConfirm").html(name);

    })
//点击事件提交订单
    $(document).on('click', '#J_Go' , function () {
        let addressId = $("#page").attr("data-current_address_id");
        if(addressId==""){
            addressId= $("#page").attr("data-default_address_id");
        }
        console.log("addressId:"+addressId);
        let finalPrice= $("#J_ActualFee").html();
        let integral = finalPrice*0.1;
        let url = "/order/addOrder";
        // 取出登录的用户
        let loginUser = getLoginUser();
        let userId = loginUser.userId;
        let params={
            userId:userId,
            addressId:addressId,
            integral:integral,
            totalPrice:finalPrice
        }
        // console.log(params);
        httpPost(url,params,function (data) {
            console.log("orderId:"+data);
            addItem(data);
        })
    })
    //把购物车商品转移到item上
    function addItem(orderId) {
        let url = "/cart/selectByUserId";
        let params={
            userId:"1",
        }
        httpGet(url,params,function (data) {
            renderingItemList(data,orderId);
        })
    }

    function renderingItemList(cartList,orderId) {
        let url = "/item/addItem";
        let isSuccess=true;
        $.each(cartList, function (index, cart) {
            let params={
                productId:cart.product.productId,
                orderId:orderId,
                itemNum:cart.num,
                itemTotalPrice:cart.totalPrice
            }
            // console.log(params)
            httpPost(url,params,function (data) {
                console.log(data)
                if(data===null){
                    isSuccess=false;
                }
            })
        })
        if(isSuccess===true){
            clearCart(orderId);
        }
    }
    //清空购物车
    function clearCart(orderId) {
        let url = "/cart/deleteAllCartByUserId";
        let loginUser = getLoginUser();
        let userId = loginUser.userId;
        let params={
            userId:userId,
        }
        httpGet(url,params,function (data) {
            console.log("clearCart:"+data);
        })
        location.href="addOrderSuccess.html?orderId="+orderId;
    }






})
//遍历商品列表
function getCartList() {
    let url = "/cart/selectByUserId";
    // 取出登录的用户
    let loginUser = getLoginUser();
    let userId = loginUser.userId;
    let params={
        userId:userId
    }
    httpGet(url,params,function (data) {
        renderingCartList(data);
    })
}
function renderingCartList(cartList) {
    let templateCartListHtml = $("#template_item").html();
    let cartListHtml = "";
    let tempCartListHtml;
    let count=0;
    //遍历购物车列表
    $.each(cartList,function (index,cart) {
        //后台数据填充到模板
        tempCartListHtml=templateCartListHtml.replace(/{name}/g, cart.product.name)
            .replace(/{price}/g,cart.product.price)
            .replace(/{info}/g,cart.product.info)
            .replace(/{cartId}/g,cart.cartId)
            .replace(/{num}/g,cart.num)
            .replace(/{totalPrice}/g,cart.totalPrice)
            .replace(/{picture}/g,cart.product.picture);
        cartListHtml+=tempCartListHtml;
        count+=cart.totalPrice;
    });
    $(".finalPrice").html(count)

    $("#tr_item").after(cartListHtml);
}
//遍历地址
function getAddressList() {
    let url = "/address/selectAddressByUserId";
    // 取出登录的用户
    let loginUser = getLoginUser();
    let userId = loginUser.userId;
    let params={
        userId:userId
    }
    httpGet(url,params,function (data) {
        renderingAddressList(data);
    })
}
function renderingAddressList(AddressList) {
    let templateAddressListHtml = $("#template_address").html();
    let AddressListHtml = "";
    let tempAddressListHtml;
    let isDefault =0;
    //遍历地址列表
    $.each(AddressList,function (index,address) {
        //后台数据填充到模板
        tempAddressListHtml=templateAddressListHtml.replace(/{name}/g, address.name)
            .replace(/{detail}/g,address.detail)
            .replace(/{phone}/g,address.phone)
            .replace(/{addressId}/g,address.addressId);

        AddressListHtml+=tempAddressListHtml;
        //默认第一个地址
        if(isDefault===0){
            $("#page").attr("data-default_address_id",address.addressId)
            $("#J_AddrConfirm").html(address.detail);
            $("#J_AddrNameConfirm").html(address.name);
            isDefault=1;
        }

    });



    $("#address-list").html(AddressListHtml);
}